package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectPaper;
import com.EM1.www.po.Teacher;
import com.EM1.www.util.Arrayparse;
import com.EM1.www.util.UpdateSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/CreatPaper")
public class CreatPaper extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String papertitle = request.getParameter("papertitle");
        if(papertitle==null){
            request.setAttribute("warn","试卷标题不为空");
        }else {
            String[] questionids = request.getParameterValues("questionid");
            if(questionids==null){
                request.setAttribute("warn","题目不为空");
            }else {
                ArrayList<Integer> questionid = Arrayparse.intparse(questionids);
                ConnectPaper connectPaper=new ConnectPaper();
                Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
                try {
                    connectPaper.insertpaper(teacher.getId(),papertitle,questionid);
                    UpdateSession.updatePapers(request);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                request.getRequestDispatcher("paper.jsp").forward(request,response);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
